/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*/

#ifndef NEARFIELDTARGETHANDLER_H
#define NEARFIELDTARGETHANDLER_H

#include <QNearFieldManager>
#include <QtCore/QObject>

QTM_USE_NAMESPACE

class SharedMemory;

class NearFieldItem : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(NearFieldItem)
    Q_ENUMS(TargetAccessMode ErrorCategory Error)
    Q_PROPERTY(TargetAccessMode targetAccessMode READ targetAccessMode WRITE setTargetAccessMode NOTIFY targetAccessModeChanged)
    Q_PROPERTY(QString ndefRecordType READ ndefRecordType WRITE setNdefRecordType NOTIFY ndefRecordTypeChanged)
    Q_PROPERTY(QString ndefRecordPayload READ ndefRecordPayload WRITE setNdefRecordPayload NOTIFY ndefRecordPayloadChanged)
    Q_PROPERTY(ErrorCategory errorCategory READ errorCategory NOTIFY errorChanged)
    Q_PROPERTY(int error READ error NOTIFY errorChanged)

public:
    enum TargetAccessMode {
        NoTargetAccess = QNearFieldManager::NoTargetAccess,
        NdefReadTargetAccess = QNearFieldManager::NdefReadTargetAccess,
        NdefWriteTargetAccess = QNearFieldManager::NdefWriteTargetAccess
    };

    enum ErrorCategory {
        NoErrorCategory,
        NearFieldManagerErrorCategory,
        NearFieldTargetErrorCategory,
        SharedMemoryErrorCategory,
        UnknownErrorCategory
    };

    enum Error {
        NoError,
        NearFieldManagerNotAvailableError,
        NearFieldManagerUnableToStartTargetDetectionError
    };

    explicit NearFieldItem(QObject *parent = 0);
    ~NearFieldItem();

    Q_INVOKABLE TargetAccessMode targetAccessMode() const;
    Q_INVOKABLE QString ndefRecordType() const;
    Q_INVOKABLE QString ndefRecordPayload() const;
    Q_INVOKABLE ErrorCategory errorCategory() const;
    Q_INVOKABLE int error() const;

public Q_SLOTS:
    void setTargetAccessMode(TargetAccessMode targetAccessMode);
    void setNdefRecordType(const QString &ndefRecordType);
    void setNdefRecordPayload(const QString &ndefRecordPayload);
    void readSharedMemory();

Q_SIGNALS:
    void targetAccessModeChanged(TargetAccessMode targetAccessMode);
    void ndefRecordTypeChanged(const QString &ndefRecordType);
    void ndefRecordPayloadChanged(const QString &ndefRecordPayload);
    void errorChanged(ErrorCategory errorCategory, int error);

    void targetDetected();
    void targetLost();
    void ndefMessageRead();
    void ndefMessageWritten();

private Q_SLOTS:
    void handleSharedMemoryError(int error);
    void handleTargetDetected(QNearFieldTarget *target);
    void handleTargetLost(QNearFieldTarget *target);
    void handleTargetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id);
    void handleNdefMessageRead(const QNdefMessage &message);
    void handleNdefMessagesWritten();

private:
    void readNdefMessages(QNearFieldTarget *target);
    void writeNdefMessage(QNearFieldTarget *target);
    void setError(ErrorCategory errorCategory, int error);

private:
    SharedMemory *m_sharedMemory;
    QNearFieldManager *m_nearFieldManager;
    TargetAccessMode m_targetAccessMode;
    QString m_ndefRecordType;
    QString m_ndefRecordPayload;
    ErrorCategory m_errorCategory;
    int m_error;
};

#endif // NEARFIELDTARGETHANDLER_H
